/**
 * purpose：common method package
 * author：fengmuli
 * time:2018~
 * address:guangankemao
 * @param formatTime 时间格式化
 ----------------------------------------*/
// 字符前是否添加0
const formatNum = function (n) {
  n = n.toString();
  return n[1] ? n : "0" + n;
};
class Tools {
  formatTime(date, time = 1, format = "-") {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    if (time == 1) {//2019-09-09 12:09:09:09
      return (
        [year, month, day].map(formatNum).join(format) +
        " " +
        [hour, minute, second].map(formatNum).join(":")
      );
    } else if (time == 2) {//2019-09-09
      return [year, month, day].map(formatNum).join(format);
    } else if (time == 8) {//星期
      var week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
      return week[date.getDay()];
    } else if (time == 7) {
      return [year, month, day]
    } else {//12:09:09
      return [hour, minute, second].map(formatNum).join(":");
    }
  }
  confirmTips(pageobj, obj, callback) {
    pageobj.$confirm(obj.message, obj.title ? obj.title : "提示", {
        confirmButtonText: obj.confirmButtonText
          ? obj.confirmButtonText
          : "确定",
        cancelButtonText: obj.cancelButtonText ? obj.cancelButtonText : "取消",
        type: obj.type ? obj.type : "warning",
        center: obj.center,
        showCancelButton: obj.showCancelButton,
        showConfirmButton: obj.showConfirmButton,
        dangerouslyUseHTMLString: obj.html ? obj.html : false
      })
      .then(res => {
        if (callback && typeof callback === "function") {
          callback(res);
        }
      })
      .catch(err => {
        console.log(err);
      });
  }
   // 删除对象中的空值
   dealObjectValue(obj) {
    var param = {};
    if (obj === null || obj === undefined || obj === "") return param;
    for (var key in obj) {
      if (typeof obj[key] === "object" && Object.keys(obj[key]).length != 0) {
        param[key] = this.dealObjectValue(obj[key]);
      } else if (
        typeof obj[key] !== "object" &&
        obj[key] !== null &&
        obj[key] !== undefined &&
        obj[key] !== ""
      ) {
        param[key] = obj[key];
      }
    }
    return param;
  }
  // 节流防抖
  throttling(fn, wait, immediate) {
    let timer,
      timeStamp = 0;
    let context, args;
    let run = () => {
      timer = setTimeout(() => {
        if (!immediate) {
          fn.apply(context, args);
        }
        clearTimeout(timer);
        timer = null;
      }, wait);
    };
    return function() {
      context = this;
      args = arguments;
      if (!timer) {
        if (immediate) {
          fn.apply(context, args);
        }
        run();
      } else {
        // console.log("throttle, ignore");
      }
    };
  }
}
const fmlCom=new Tools()
export default fmlCom
